package org.jeecg.modules.hotel.belongToSkd.job;

import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.hotel.belongToSkd.entity.LwgyStudentBed;
import org.jeecg.modules.hotel.belongToSkd.entity.ThirdStandard;
import org.jeecg.modules.hotel.belongToSkd.service.ThirdStandardChangeService;
import org.jeecg.modules.hotel.belongToSkd.service.ThirdStandardService;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 标准计费信息表专门用来同步标签计费相关的数据,床位价格
 * 它依赖学生学籍信息，每天推送一次到山科大中间库(third_standard表)
 * @author:MJ-BaBa
 * @date: 2021.07.15
 */

@Slf4j
public class ThirdStandardJob implements Job {

    @Autowired
    private ThirdStandardService thirdStandardService;
    @Autowired
    private ThirdStandardChangeService thirdStandardChangeService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        System.out.println("============进入定时器=============");
        long start1 = System.currentTimeMillis();
        //所有学生的住宿信息，
        List<LwgyStudentBed> studentList = thirdStandardService.queryStuRoom();

        long start2 = System.currentTimeMillis();
        log.info("powerList  耗时：" + (start2 - start1));

        List<ThirdStandard> roomList = new ArrayList<>();
        //循环每个学生的信息,将信息保存到roomList中
        studentList.forEach(item -> {
            ThirdStandard thirdStandard = new ThirdStandard();
            java.util.Calendar calendar = java.util.Calendar.getInstance();
            int year = calendar.get(java.util.Calendar.YEAR);
            int month = calendar.get(Calendar.MONTH) + 1;
            int schoolYear = 0;
            if (oConvertUtils.isNotEmpty(item.getStudentYear())) {
                schoolYear = year - Integer.parseInt(item.getStudentYear());
                if (month > 8 && month <= 12) {
                    schoolYear = schoolYear + 1;
                } else if (1 <= month && month < 8) {
                    schoolYear = schoolYear;
                }
            }
            thirdStandard.setPersonnelName(item.getStudentName());
            thirdStandard.setPersonnelNo(item.getStudentNumber());
            thirdStandard.setStandardTypeNo("ZS");
            thirdStandard.setStandardNo(item.getBedPrice());
            thirdStandard.setTransCount(BigDecimal.valueOf(1));
            thirdStandard.setUseDate(item.getUpdateTime());
            thirdStandard.setCreatedTime(item.getCreateTime());
            thirdStandard.setLastUpdatedTime(item.getUpdateTime());
            thirdStandard.setPeriodType("11");
            thirdStandard.setPeriodYear(String.valueOf(year));
            thirdStandard.setPeriodSort("01");
            thirdStandard.setFeesPeriodYear(String.valueOf(year));
            thirdStandard.setFeesPeriodSort("01");
            thirdStandard.setEducationalBackground(item.getStudentEducation());
            roomList.add(thirdStandard);

        });
        // 批量添加学生住宿信息
        int insertLength = roomList.size();
        int i = 0;
        while(insertLength > 500){
            thirdStandardChangeService.insertHotelInfo(roomList.subList(i,i + 500));
            i = i + 500;
            insertLength = insertLength - 500;
        }
        if(insertLength > 0){
            thirdStandardChangeService.insertHotelInfo(roomList.subList(i,i + insertLength));
        }

    }
}
